package web;

/**
 * @author Lenovo
 * @date 2022/1/5
 */
//用JFrame做页面

import com.Dao.Question;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class LoginFrame{

    private static JPanel panel = null;
    private static JFrame frame = null;

    public static  JLabel  questionLabel = new JLabel("",JLabel.CENTER);
    public static  JButton sureButton = new JButton("确定");
    public static  JButton cancelButton = new JButton("取消");
    public static  JTextField accountArea = new  JTextField(20);
    public static  JLabel accountInfoLabel = new JLabel("",JLabel.CENTER);
    public static  JTextField  passwordArea = new  JTextField(20);
    public static  JLabel  passwordInfoLabel = new JLabel("",JLabel.CENTER);
    public static  JTextField  inputAnswerArea = new  JTextField(20);
    public static  JLabel  answerInfoLabel = new JLabel("",JLabel.CENTER);


    public  JPanel loginView(){
        //导入frame
        frame = MainFrame.returnJFrame();
        panel = new JPanel(new BorderLayout());
        //添加新的panel
        frame.add(panel);

        // panel内部设计
        //=========================== topPanel start ============================//
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout());
        JLabel label = new JLabel("档案查询系统登陆",JLabel.CENTER);
        label.setFont(new Font("宋体",Font.BOLD,32));
        topPanel.add(label);
        topPanel.setPreferredSize(new Dimension(webUtil.PANEL_WIDTH, webUtil.PANEL_HEIGHT/5));
        panel.add(topPanel, BorderLayout.NORTH);
        //=========================== topPanel end ============================//

        //=========================== subPanel start ============================//
        JPanel subPanel = new JPanel();
        subPanel.setSize(new Dimension(webUtil.PANEL_WIDTH, webUtil.PANEL_HEIGHT*3/5));
        GridLayout grid = new  GridLayout(3,3);
        grid.setHgap(20);
        grid.setVgap(10);
        subPanel.setLayout(grid);

        FlowLayout flow = new FlowLayout();
        flow.setVgap(40);  //设置组件之间的水平间距
        ///////////
        //  first floor
        //////////
        JLabel accountLabel = new JLabel("账户:",JLabel.CENTER);
        accountLabel.setFont(new Font("宋体",Font.BOLD,20));
        subPanel.add(accountLabel);
            JPanel subPanel1 = new JPanel(flow);
            subPanel1.setSize(new Dimension(webUtil.PANEL_WIDTH/3, webUtil.PANEL_HEIGHT/5));

            accountArea.setFont(new Font("宋体",Font.BOLD,20));
            accountArea.setDocument(new MyDocument(20));
            accountArea.setPreferredSize(new Dimension (20,45));
            subPanel1.add(accountArea);
//            subPanel1.setBorder(new TitledBorder(new EtchedBorder(), "测试")); //测试用
        subPanel.add(subPanel1);

        accountInfoLabel.setFont(new Font("宋体",Font.BOLD,20));
        subPanel.add(accountInfoLabel);

        ///////////
        //  second floor
        //////////
        JLabel passwordLabel = new JLabel("口令:",JLabel.CENTER);
        passwordLabel.setFont(new Font("宋体",Font.BOLD,20));
        subPanel.add(passwordLabel);
            JPanel subPanel2 = new JPanel(flow);
            subPanel1.setSize(new Dimension(webUtil.PANEL_WIDTH/3, webUtil.PANEL_HEIGHT/5));

            passwordArea.setFont(new Font("宋体",Font.BOLD,20));
            passwordArea.setPreferredSize(new Dimension (20,45));
            passwordArea.setDocument(new MyDocument(20));
            subPanel2.add(passwordArea);
//            subPanel2.setBorder(new TitledBorder(new EtchedBorder(), "测试")); //测试用
            subPanel.add(subPanel2);

        passwordInfoLabel.setFont(new Font("宋体",Font.BOLD,20));
        subPanel.add(passwordInfoLabel);

        ///////////
        //  third floor
        //////////
        new Question().newGame();
        String tmpString = new Question().toString();
        questionLabel.setText(tmpString+" = ");
        questionLabel.setFont(new Font("宋体",Font.BOLD,20));
        subPanel.add(questionLabel);
            JPanel subPanel3 = new JPanel(flow);
            subPanel1.setSize(new Dimension(webUtil.PANEL_WIDTH/3, webUtil.PANEL_HEIGHT/5));

            inputAnswerArea.setFont(new Font("宋体",Font.BOLD,20));
            inputAnswerArea.setPreferredSize(new Dimension (20,45));
            inputAnswerArea.setDocument(new MyDocument(20));
            subPanel3.add(inputAnswerArea);
    //        subPanel3.setBorder(new TitledBorder(new EtchedBorder(), "测试")); //测试用
            subPanel.add(subPanel3);

        answerInfoLabel.setFont(new Font("宋体",Font.BOLD,20));
        subPanel.add(answerInfoLabel);
        panel.add(subPanel, BorderLayout.CENTER);
        //=========================== subPanel end ============================//

        //=========================== bottomPanel start ============================//
        JPanel bottomPanel = new JPanel();
        FlowLayout bottomFlow = new FlowLayout();
        bottomFlow.setHgap(100);  //设置组件之间的水平间距
        bottomPanel.setLayout(bottomFlow);

        sureButton.setFont(new Font("宋体",Font.BOLD,22));
        sureButton.setPreferredSize(new Dimension(120,60));

        cancelButton.setPreferredSize(new Dimension(120,60));
        cancelButton.setFont(new Font("宋体",Font.BOLD,22));
        bottomPanel.add(sureButton);
        bottomPanel.add(cancelButton);

        bottomPanel.setPreferredSize(new Dimension(webUtil.PANEL_WIDTH, webUtil.PANEL_HEIGHT/5));
        panel.add(bottomPanel, BorderLayout.SOUTH);
        //=========================== bottomPanel end ============================//

        //============================ Listener start =============================//
        accountArea.getDocument().addDocumentListener(
                new   DocumentListener() {
                    @Override
                    public void insertUpdate(DocumentEvent e) {accountInfoLabel.setText("");}
                    @Override
                    public void removeUpdate(DocumentEvent e) {accountInfoLabel.setText("");}
                    @Override
                    public void changedUpdate(DocumentEvent e) {
                        accountInfoLabel.setText("");
                    }
                }
        );
        passwordArea.getDocument().addDocumentListener(
                new   DocumentListener() {
                    @Override
                    public void insertUpdate(DocumentEvent e) {passwordInfoLabel.setText("");}
                    @Override
                    public void removeUpdate(DocumentEvent e) {passwordInfoLabel.setText("");}
                    @Override
                    public void changedUpdate(DocumentEvent e) {
                        passwordInfoLabel.setText("");
                    }
                }
        );
        inputAnswerArea.getDocument().addDocumentListener(
                new   DocumentListener() {
                    @Override
                    public void insertUpdate(DocumentEvent e) {answerInfoLabel.setText("");}
                    @Override
                    public void removeUpdate(DocumentEvent e) {answerInfoLabel.setText("");}
                    @Override
                    public void changedUpdate(DocumentEvent e) {answerInfoLabel.setText("");
                    }
                }
        );
        //============================ Listener end =============================//
        return panel;
    }
    public static void updateQuestion(){
        new Question().newGame();
        String tmpString = new Question().toString();
        questionLabel.setText(tmpString);
    }
}
